package com.mc.string;

public class BM2 {
	
	private static int[] right;
	public static void main(String[] args){
		String tar = "abcjdjskladjk";
		String pattern = "djsk";
		int m = tar.length();
		int n = pattern.length();
		computeRight(pattern);
		int skip;
		for(int i=0;i<=m-n;i+=skip){
			skip=0;
			for(int j=n-1;j>=0;j--){
				if(tar.charAt(i+j)!=pattern.charAt(j)){
					skip = j-right[tar.charAt(i+j)];
					if(skip<1)skip=1;
					break;
				}
			}
			if(skip==0){
				//i为第一次匹配位置
				System.out.println("s："+i);
				break;
			}
		}
		System.out.println("fail");
		
	}
	
	public static void computeRight(String pattern){
		right = new int[256];
		
		for(int i=0;i<256;i++)
			right[i] = -1;
		for(int i=0;i<pattern.length();i++){
			char ch = pattern.charAt(i);
			right[ch] = i;
		}
	}
	
	
	
}
